package com.jasmine.bjstkserver;

import cn.hutool.core.util.StrUtil;
import com.jasmine.bjstkserver.core.config.annotation.AuthIgnoreLimiter;
import com.jasmine.core.constants.BjsConstants;
import com.jasmine.core.dto.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @author jasmineXz
 */
@EnableAsync
@RestController
@EnableScheduling
@SpringBootApplication
@MapperScan("com.jasmine.bjstkserver.mapper")
public class BjstkServerApplication {
    private static final Logger log = LoggerFactory.getLogger(BjstkServerApplication.class);

    private static Environment env;

    public static void main(String[] args) {
        try {
            SpringApplication.run(BjstkServerApplication.class, args);
            successPrint();
        } catch (Exception e) {
            faultPrint();
        }
    }

    @RequestMapping
    @AuthIgnoreLimiter
    public R bjstkHome() {
        return R.ok("这里是 「FOREST SECRET」 服务器");
    }

    @Autowired
    public void setEnv(Environment env) {
        BjstkServerApplication.env = env;
    }

    private static void successPrint () {
        log.info(
                "\n┌───────────────────────────────────────────┐"+
                "\n|       BjstkServer Starting Success        |"+
                "\n└───────────────────────────────────────────┘"+
                "\nPORT         : {}" +
                "\nENVIRONMENT  : {}" +
                "\nFS_VERSION   : {}" +
                "\nDESCRIPTIONS : {}"
        ,
        env.getProperty("server.port"),
        env.getProperty("spring.profiles.active"),
        env.getProperty("fs-version"),
        StrUtil.equalsIgnoreCase(env.getProperty("spring.profiles.active"), BjsConstants.DEV_DB_NAME) ? BjsConstants.DEV_INFO : BjsConstants.PROD_WARNING);
    }

    private static void faultPrint () {
        log.info(
                "\n┌───────────────────────────────────────────┐"+
                "\n|        BjstkServer Starting fault         |"+
                "\n└───────────────────────────────────────────┘"+
                "\nplease contact us" +
                "\nDEVELOPER : {}" +
                "\nGITEE     : {}"
        ,
        "https://gitee.com/jasminexz","https://gitee.com/BJSTeam/fs-main");
    }
}
